package org.his.controller;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.spi.RegisterableService;

import org.his.entity.Drug;
import org.his.entity.Registration;
import org.his.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/registration")
public class RegistrationController {
	@Autowired
	private RegistrationService registrationService;
	
	@RequestMapping(value = "/newmedicalrecord")
	@ResponseBody
	public Map<String, Object> newMedicalrecord(Registration registration) {
		 Map<String, Object> map = new HashMap<String, Object>();
		String cString = registrationService.queryCountWait(registration);
		if (cString != null) {
			String bString = registration.getDoctorno();
			String aString = registration.getRegistrationdate();
			String medicalrecord = registrationService.newMedicalrecord(aString, bString, cString);
			registration.setMedicalrecord(medicalrecord);
			registration.setWaitno(cString);
			 
			 map.put("registration", registration);
			return map;
		}
		else {
			map.put("registration", registration);
			return map;
		}
	}
	@RequestMapping(value = "/addregistration", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addRegistration(Registration addRegistration, ModelMap model) {		
			int count = 0;
			count = registrationService.addRegistration(addRegistration);
			if (count == 1) {
				return "{\"success\":\"true\",\"message\":\"挂号成功，请支付诊金\"}";
			}else {
			return "{\"success\":\"false\",\"message\":\"挂号失败\"}";
		}
	}
	@RequestMapping("/querywaitpatient")
	@ResponseBody
	public Collection<Registration> queryWaitPatient(@RequestParam("doctorno")String doctorno, Registration queryRegistration){
		if (queryRegistration.getDoctorno() == null) {
			queryRegistration.setDoctorno(doctorno);
		}
		List<Registration> qRegistrations= registrationService.queryWaitPatientByFrom(queryRegistration);		
		return qRegistrations;
	}
	@RequestMapping("/querypatient")
	@ResponseBody
	public Collection<Registration> queryPatient(Registration queryRegistration){		
		List<Registration> qRegistrations= registrationService.queryWaitPatientByFrom(queryRegistration);		
		return qRegistrations;
	}
}
